package scau.erps.view.base;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import scau.erps.app.Erps;
import scau.erps.entity.Factory;
import scau.erps.entity.Line;
import scau.erps.entity.Team;

/**
 * 新建生产线对话框
 * 
 * @author outsider
 * 
 * 
 */

public class BuyLineDialog extends LineDialog {
	private static final long serialVersionUID = 1L;
	private List<Factory> listFactory = factoryService.getFactoyByTeamId(team_Id);
	private List<Line> listLine = lineService.getLineByTeamId(team_Id);
	private DecisionPanel superPanel;

	public BuyLineDialog(DecisionPanel superPanel) {
		super();
		this.okButton.setText("确定购买");
		this.superPanel = superPanel;
		setTitle("投资新生产线");
		// 获取该小组可购买生产线
		for (int i = 0; i <= 7; i++) {
			Boolean bo = null;
			if (i >= 0 && i <= 3)
				bo = judge(listFactory.get(0), listLine.get(i));
			if (i >= 4 && i <= 6)
				bo = judge(listFactory.get(1), listLine.get(i));
			if (i == 7)
				bo = judge(listFactory.get(2), listLine.get(i));
			typeComboBox[i].setEnabled(bo);
			typeComboBox[i].addItemListener(new ItemListener() {		
				public void itemStateChanged(ItemEvent e) {
					int i;
			        for (i=0;i<=7;i++){
			        	if (e.getSource().equals(typeComboBox[i])) break;
			        }
			        if (typeComboBox[i].getSelectedIndex()==4){
			        	productComboBox[i].setSelectedIndex(0);
			        	productComboBox[i].setEnabled(false);  	
			        }
			        else{
			        	productComboBox[i].setEnabled(true);
			        }
				}
			});
			productComboBox[i].setEnabled(bo);
		}
		// 确认按钮
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				okButtonAction();
			}
		});
		// 更新按钮
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateButtonAction();
			}
		});
		// 高利贷
		usuriousLoanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new UsuriousLoanDialog(getSuperPanel());
			}
		});
		// 租用厂房
		rentFactoryButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Team team = Erps.getCurrentTeam();
				List<Factory> list = factoryService.getTeamFactoyByStatus(team.getId(), "未购买"); // 获取当前小组可租用厂房
				if (list.isEmpty() == false) {
					List<String> type = new ArrayList<>();
					for (Factory factory : list)
						type.add(factory.getType());
					String s = (String) JOptionPane.showInputDialog(null, "请选择租用厂房类型:\n", "租用厂房", JOptionPane.PLAIN_MESSAGE, null, type.toArray(), type.get(0));
					if (s != null) {
						factoryService.rentFactoy(team.getId(), s);
						JOptionPane.showMessageDialog(null, "租用" + s + "成功", "提示", JOptionPane.INFORMATION_MESSAGE);
					}
				} else
					JOptionPane.showMessageDialog(null, "当前无可租用厂房", "提示", JOptionPane.INFORMATION_MESSAGE);
				listFactory = factoryService.getFactoyByTeamId(team_Id);
				for (int i = 0; i <= 7; i++) {
					Boolean bo = null;
					if (i >= 0 && i <= 3)
						bo = judge(listFactory.get(0), listLine.get(i));
					if (i >= 4 && i <= 6)
						bo = judge(listFactory.get(1), listLine.get(i));
					if (i == 7)
						bo = judge(listFactory.get(2), listLine.get(i));
					typeComboBox[i].setEnabled(bo);
					productComboBox[i].setEnabled(bo);
				}
			}
		});
	}
	// 通过参数判断该位置是否可购买新线
	private Boolean judge(Factory factory, Line line) { 
		if (factory.getStatus().equals("未购买"))
			return false;
		if (!line.getStatus().equals("空置"))
			return false;
		return true;
	}
	// “确认”按钮操作：新购生产线
	private void okButtonAction() { 
		int count = 0;
		for (int i = 0; i <= 7; i++) {
			if (typeComboBox[i].isEnabled() && typeComboBox[i].getSelectedIndex() != 0)
				if (productComboBox[i].getSelectedIndex() != 0||typeComboBox[i].getSelectedItem().equals("柔性线")) {
					lineService.buyLine(team_Id, i + 1, PRODUCT[productComboBox[i].getSelectedIndex()], TYPE[typeComboBox[i].getSelectedIndex()]);
					count++;
				} else {
					JOptionPane.showMessageDialog(null, "请为新建生产线指定产品类型", "提示", JOptionPane.INFORMATION_MESSAGE);
					return;
				}
		}
		JOptionPane.showMessageDialog(null, "本季度新建" + count + "条生产线，请从设备商出领取对应生产线和产品牌。"+"\n请将生产线反向放置在盘面上，产品牌放在生产线上方"+"\n请点击支付费用按钮完成新建。", "提示", JOptionPane.INFORMATION_MESSAGE);
		okButton.setEnabled(false);
		updateButton.setEnabled(true);
	}
	private void updateButtonAction() {
		int[] tot = lineService.updateBuyLine(team_Id);
		if (tot[0] >= 0) {
			String message = "";
			if (tot[1] > 0)
				message = message + "手工线建设费用" + tot[1] + "M" + "\n";
			if (tot[2] > 0)
				message = message + "半自动建设费用" + tot[2] + "M" + "\n";
			if (tot[3] > 0)
				message = message + "全自动建设费用" + tot[3] + "M" + "\n";
			if (tot[4] > 0)
				message = message + "柔性线建设费用" + tot[4] + "M" + "\n";
			JOptionPane.showMessageDialog(null, message + "本季度为建设中生产线支出" + tot[0] + "M。"+"\n请取现金放入对应生产线下方净值区域", "提示", JOptionPane.INFORMATION_MESSAGE);
			superPanel.getDashBoardPanel().setFactoryData();
			superPanel.getDashBoardPanel().setDashBoardData(15);
			dispose();
		} else
			JOptionPane.showMessageDialog(null, "当前现金不足，请进行融资操作", "提示", JOptionPane.INFORMATION_MESSAGE);

	}

	private DecisionPanel getSuperPanel() {
		return this.superPanel;
	}
}
